package com.fancy.learncenter.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.CartoonTeacherReportDataBean;
import com.fancy.learncenter.utils.LogUtil;
import com.kelin.scrollablepanel.library.PanelAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kelin on 16-11-18.
 */

public class ScrollablePanelAdapter extends PanelAdapter {
    private static final int TITLE_TYPE = 4;
    private static final int ROOM_TYPE = 0;
    private static final int HORIZONTAL_TYPE = 1;
    private static final int ORDER_TYPE = 2;

    private List<CartoonTeacherReportDataBean.ResultBean.StatisticListBean> roomInfoList = new ArrayList<>();
    private List<String> horizontalInfoList = new ArrayList<>();
    private List<List<String>> ordersList = new ArrayList<>();


    @Override
    public int getRowCount() {
        return roomInfoList.size() + 1;
    }

    @Override
    public int getColumnCount() {
        return horizontalInfoList.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int row, int column) {
        int viewType = getItemViewType(row, column);
        switch (viewType) {
            case HORIZONTAL_TYPE:
                setHorizontalView(column, (HorizontalViewHolder) holder);
                break;
            case ROOM_TYPE:
                setRoleView(row, (RoleViewHolder) holder);
                break;
            case ORDER_TYPE:
                setOrderView(row, column, (OrderViewHolder) holder);
                break;
            case TITLE_TYPE:
                break;
            default:
                setOrderView(row, column, (OrderViewHolder) holder);
        }
    }

    public int getItemViewType(int row, int column) {
        if (column == 0 && row == 0) {
            return TITLE_TYPE;
        }
        if (column == 0) {
            return ROOM_TYPE;
        }
        if (row == 0) {
            return HORIZONTAL_TYPE;
        }
        return ORDER_TYPE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HORIZONTAL_TYPE:
                return new HorizontalViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.fragment_cartoon_teacher_class_report_title_item, parent, false));

            case ROOM_TYPE:
                return new RoleViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.fragment_cartoon_teacher_class_report_student_item, parent, false));
            case ORDER_TYPE:
                return new OrderViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.fragment_cartoon_teacher_class_report_subject_item, parent, false));
            case TITLE_TYPE:
                return new TitleViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.fragment_cartoon_teacher_class_report_name, parent, false));
            default:
                break;
        }
        return new OrderViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_cartoon_teacher_class_report_subject_item, parent, false));
    }


    private void setHorizontalView(int pos, HorizontalViewHolder viewHolder) {
        viewHolder.title.setText(horizontalInfoList.get(pos));
    }

    private void setRoleView(int pos, RoleViewHolder viewHolder) {
        CartoonTeacherReportDataBean.ResultBean.StatisticListBean roomInfo = roomInfoList.get(pos - 1);
        if (roomInfo != null && pos > 0) {
            viewHolder.simpleDraweeView.setImageURI(roomInfo.getStudentImgurl());
            viewHolder.role_name.setText(roomInfo.getStudentName());
        }
    }

    private void setOrderView(final int row, final int column, OrderViewHolder viewHolder) {




        String title = ordersList.get(row - 1).get(column - 1);
        viewHolder.content.setText(title);

//            if (orderInfo.getStatus() != OrderInfo.Status.BLANK) {
//                viewHolder.itemView.setClickable(true);
//                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (orderInfo.isBegin()) {
//                            Toast.makeText(v.getContext(), "name:" + orderInfo.getGuestName(), Toast.LENGTH_SHORT).show();
//                        } else {
//                            int i = 2;
//                            while (column - i >= 0 && ordersList.get(row - 1).get(column - i).getId() == orderInfo.getId()) {
//                                i++;
//                            }
//                            final
//                            Toast.makeText(v.getContext(), "name:" + info.getGuestName(), Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                });
//            } else {
//                viewHolder.itemView.setClickable(false);
//            }
    }


    class HorizontalViewHolder extends RecyclerView.ViewHolder {
        public TextView title;

        public HorizontalViewHolder(View itemView) {
            super(itemView);
            this.title = (TextView) itemView.findViewById(R.id.title);
//            this.weekTextView = (TextView) itemView.findViewById(R.id.week);
        }

    }

    class RoleViewHolder extends RecyclerView.ViewHolder {
        public SimpleDraweeView simpleDraweeView;
        public TextView role_name;

        public RoleViewHolder(View view) {
            super(view);
            this.simpleDraweeView = view.findViewById(R.id.simpleDraweeView);
            this.role_name = (TextView) view.findViewById(R.id.role_name);
        }
    }

    class OrderViewHolder extends RecyclerView.ViewHolder {
        public TextView content;
        public View view;

        public OrderViewHolder(View view) {
            super(view);
            this.view = view;
            this.content = (TextView) view.findViewById(R.id.content);
        }
    }

    class TitleViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTextView;

        public TitleViewHolder(View view) {
            super(view);
            this.titleTextView = (TextView) view.findViewById(R.id.title);
        }

    }


    public void setRoomInfoList(List<CartoonTeacherReportDataBean.ResultBean.StatisticListBean> roomInfoList) {
        this.roomInfoList = roomInfoList;
    }

    /**
     * 顶部，水平标题
     *
     * @param dateInfoList
     */
    public void setHorizontalInfoList(List<String> dateInfoList) {
        this.horizontalInfoList = dateInfoList;
    }


    public void setOrdersList(List<List<String>> ordersList) {
        this.ordersList = ordersList;
    }


}
